package com.course.servlet;

import com.course.entity.GuanliyuanAdmin;
import com.course.service.GuanliyuanAdminService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/guanliyuan_login")
public class GuanliyuanLoginServlet extends HttpServlet {
    private final GuanliyuanAdminService adminService = new GuanliyuanAdminService();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        GuanliyuanAdmin admin = adminService.login(username, password);
        if (admin != null) {
            HttpSession session = request.getSession();
            session.setAttribute("guanliyuan_admin", admin);
            session.setAttribute("username", admin.getUsername());
            response.sendRedirect("guanliyuan_index.jsp");
        } else {
            request.setAttribute("error", "用户名或密码错误");
            request.getRequestDispatcher("guanliyuan_login.jsp").forward(request, response);
        }
    }
} 